package com.aaa.mrcai.service.impl;

import com.aaa.mrcai.mapper.RoleMapper;
import com.aaa.mrcai.pojo.entity.Location;
import com.aaa.mrcai.pojo.entity.Role;
import com.aaa.mrcai.pojo.entity.RoleChildren;
import com.aaa.mrcai.pojo.entity.State;
import com.aaa.mrcai.pojo.vo.RoleVO;
import com.aaa.mrcai.query.RoleQuery;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:Li-Yan-Chun
 * @Date:2021/12/15 22:39
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public AjaxResult list(RoleQuery roleQuery) {
        List<Role> list = roleMapper.list(roleQuery);
        Long aLong = roleMapper.queryTotalCount(roleQuery);
        Map<String, Object> map = new HashMap<>();
        map.put("currentPageData",list);
        map.put("currentDataTotal",aLong);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getState() {
        List<State> state = roleMapper.getState();
        return AjaxResult.success(state);
    }

    @Override
    public AjaxResult roleList() {
        List<RoleChildren> roleChildren = roleMapper.roleList();
        return AjaxResult.success(roleChildren);
    }

    @Override
    public AjaxResult add(RoleVO roleVO) {
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        roleVO.setCreateDate(date.format(formatter));
        int i = roleMapper.add(roleVO);
        if (i>0){
            return AjaxResult.success(roleVO.getId());
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult addPerm(RoleVO roleVO) {
        int i = roleMapper.addPerm(roleVO);
        if (i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult getOne(int id) {
        RoleVO roleVO = roleMapper.getOne(id);
        List<RoleVO> role = roleMapper.getRole(id);

        int [] arr = new int[role.size()];
        for(int i = 0;i<role.size();i++){
            arr[i] = role.get(i).getPid();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("role",roleVO);
        map.put("arr",arr);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult updateRole(RoleVO roleVO) {
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        roleVO.setCreateDate(date.format(formatter));
        int i = roleMapper.updateRole(roleVO);
        if (i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult deletePerm(int id) {
        int i = roleMapper.deletePerm(id);
        if (i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult deleteRole(int id) {
        int i = roleMapper.deleteRole(id);
        if (i>0){
            roleMapper.deletePerm(id);
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }
}
